package cn.kgc.vue.controller;

import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.entity.Schedule;
import cn.kgc.vue.exceptions.BusinessException;
import cn.kgc.vue.response.ResponseResult;
import cn.kgc.vue.response.ResponseStatus;
import cn.kgc.vue.service.ScheduleService;
import jakarta.annotation.Resource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

/**
 * @author 课工场
 * @date 2024/10/7
 * @description
 */
@RestController
@RequestMapping("schedule")
public class ScheduleController {

    @Resource
    private ScheduleService scheduleService;

    @PostMapping("page")
    public ResponseResult  schedulePage(@RequestBody QueryCondition<Schedule> queryCondition){
        return scheduleService.schedulePage(queryCondition);
    }

    @PostMapping("addOrUpdate")
    public ResponseResult  addOrUpdateSchedule(@RequestBody Schedule schedule){
        try {
            scheduleService.saveOrUpdate(schedule);
            return ResponseResult.success();
        }catch (DuplicateKeyException e){
            throw  new BusinessException(ResponseStatus.ERROR.getCode(), schedule.getClassNo()+"课程编号重复...");
        }

    }

    @DeleteMapping("delete/{id}")
    public ResponseResult deleteSchedule(@PathVariable("id") Integer id){

        scheduleService.removeById(id);
        return ResponseResult.success();
    }

}
